<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 微信第三方开放平台开放-接入
 */
namespace app\system\controller\event;
use think\Controller;
use think\facade\Request;
use think\facade\Log;
use filter\Filter;
use Exception;
use EasyWeChat\OpenPlatform\Server\Guard;
use app\common\facade\WechatMp;

class WechatOpen extends Controller{

    /**
     * 微信开放平台推送车票(1次/10分钟)
     * 有了车票要保存下来,获取授权时要用
     * @return json
     */
    public function ticket(){
        try {
            $server = WechatMp::openConfig()->server;
            // 处理授权成功事件
            $server->push(function ($message) {
                Log::write(json_encode($message),'auth_success');
            }, Guard::EVENT_AUTHORIZED);
            // 处理授权更新事件
            $server->push(function ($message) {
                Log::write(json_encode($message),'auth_update');
            }, Guard::EVENT_UPDATE_AUTHORIZED);
            // 处理授权取消事件
            $server->push(function ($message) {
                Log::write(json_encode($message),'auth_unauth');
            }, Guard::EVENT_UNAUTHORIZED);
            return $server->serve();
        }catch (Exception $e) {
            $this->error('授权失败');
        }
    } 
    
    /**
     * 微信开放平台事件接受
     * @return json
     */
    public function message($appid){
        try {
            if (Request::isPost()) {
                $appid  = Filter::filter_escape($appid);
                $app = WechatMp::openConfig()->officialAccount($appid);
                if ($appid == 'wx570bc396a51b8ff8') {
                    $app->server->push(function ($message) {
                        switch ($message['MsgType']) {
                        case 'event':
                            return $message['Event'].'from_callback';
                            break;
                        case 'text':
                            if ($message['Content'] == "TESTCOMPONENT_MSG_TYPE_TEXT") {
                                return 'TESTCOMPONENT_MSG_TYPE_TEXT_callback';
                            } else {
                                $authCode = explode(":", $message['Content'])[1];
                                return $authCode."_from_api";
                            }
                            break;
                        }
                    });
                } else {
                    $app->server->push(function ($message) {
                        switch ($message['MsgType']) {
                        case 'event':
                            //return '事件';
                            break;
                        case 'text':
                            //return '文本';
                            break;
                    }
                    });
                }
                $response = $app->server->serve();
                $response->send();
            }
        }catch (Exception $e) {
            $this->error('授权失败');
        }
    }
}   